package com.bianguo.android.bprinter.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bianguo.android.baselibrary.adapter.BaseRecyclerAdapter;
import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.baselibrary.view.BaseActivity;
import com.bianguo.android.bprinter.R;
import com.bianguo.android.bprinter.adapter.PrinterAdapter;
import com.bianguo.android.bprinter.utils.SmbManager;
import com.bianguo.android.ioclibrary.annotations.BindView;

import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;

import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbSession;

public class WPrintActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseRecyclerAdapter.OnClickItemListener, SmbManager.SmbListener {

    @BindView(R.id.printer_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.printer_recycler)
    RecyclerView printerRecycler;

    private LinearLayoutManager layoutManager;
    private PrinterAdapter printerAdapter;
    private SmbManager smbManager;

    @Override
    protected int layoutId() {
        return R.layout.activity_wprint;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        layoutManager = new LinearLayoutManager(mContext);
        printerRecycler.setLayoutManager(layoutManager);
        ArrayList<SmbFile> smbFiles = new ArrayList<>();
        printerAdapter = new PrinterAdapter(mContext, smbFiles);
        printerAdapter.setOnClickItemListener(this);
        printerRecycler.setAdapter(printerAdapter);
        mRefreshLayout.setOnRefreshListener(this);

        smbManager = SmbManager.getInstance();
        smbManager.setSmbListener(this);
        smbManager.scaleShareFiles("192.168.1.102");
    }

    @Override
    public void onRefresh() {
        smbManager.scaleShareFiles("192.168.1.102");
    }

    @Override
    public void onClickItem(BaseRecyclerAdapter adapter, View view, int position, int tag) {
        if (adapter instanceof PrinterAdapter) {
            ArrayList<SmbFile> list = printerAdapter.getList();
            SmbFile smbFile = list.get(position);
            switch (view.getId()) {
                case R.id.item_printer_content:
                    smbManager.doFile(smbFile);
                    break;
            }
        }
    }

    @Override
    public void showScaleShareFiles(ArrayList<SmbFile> smbFiles) {
        printerAdapter.addAll(smbFiles, true);
        mRefreshLayout.setRefreshing(false);
    }
}
